/***************************************************************************
 *   Copyright (C) 2009, 2010 by MJ   *
 *   mj_dv@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


//metamod.cpp - is a part from steamserverutil by MJ
//steamserverutil.googlecode.com

//it handles the metamod support

#include <iostream> //for cout and cin
#include <fstream> //for ifstream and ofstream
#include <stdlib.h> //for system()
#include <vector>

//for gettext
#include <libintl.h>
#include <locale.h>

#define _(STRING) gettext(STRING)

//#include "metamod.h"
#include "steamserverutil.h"
#include "conf_server.h"


//to make the differance of the arrays better 
#define OFF_N 0
#define OFF_C 1

using namespace std;



void metamod_test(unsigned short int choice)
{
	if (choice > 5)
	{
		cout << _("\n\nServer is not supported by metamod!!\n\n");
		return;
	}
	
	
	param = workdir;
	
	switch(choice)
	{
		case 1:	if(server_css==true)
		{
						
			param += "/servers_source/cstrike";
			installed = true;
		}
		else
			not_installed();
		break;
		case 2:	if(server_dods==true)
		{
			param +=  "/servers_source/dod";
											
				
			installed = true;
		}
		else
			not_installed();
		break;
		case 3:	if(server_garrysmod==true)
		{
			param +=  "/servers_source/garrysmod";
						
									
			installed = true;
		}
		else
			not_installed();
		break;
		case 4:	if(server_hl2mp==true)
		{
			param +=  "/servers_source/garrysmod";
						
									
			installed = true;
		}
		else
			not_installed();
		break;
		case 5:	if(server_tf==true)
		{	
			param +=  "/servers_source/tf";
					
			installed = true;
		}
		else
			not_installed();
		break;
		default: cout << _("\n\nTry again ;)\n\n");
		break;
	}
}

bool is_metamod_installed(string path)
{
	path += "/addons/metamod.vdf";
	path.erase(0, 3); //remove "cd "
	ifstream in(path.c_str());
	
	//cout << path;
	
	bool val = in.is_open();
	in.close();
	
	return val;
}

void install_metamod()
{
	
	cout << _("\n\nIn which server do you want to install metamod?\n\n");

	int choice = choose_dialog();
	
	if(choice == -1)
		return;

	string params; //the hole command is saved here



	params = "cd "; //because in function mani_test the "cd " is not included
	param.clear();

	
	//if it is really installed
	installed = false;
	metamod_test(server[choice]);
	params += param;
	//-------end---------(modified)


	if(installed==true)
	{
		
			
		cout << endl << endl << _("Downloading metamod with wget, unzipping archive...") << endl << endl;
		params += " && wget http://www.xfactorservers.com/sourcemod/mmsource-1.8.0.tar.gz && ";
		

		//to download metamod

		//--atime-preserve=system 

		params += "tar -xzf mmsource-1.8.0.tar.gz ";
		//to "unzip" metamod (unzips automatically in right folder)

		system_check(system(params.c_str()));		
		

		cout << _("\n\nMetaMod succesfully installed!\n");
		

	}
}

void deactivate_metamod()
{
	cout << _("\n\nFrom which server do you wish to deactivate metamod? \n");
	
	int choice = choose_dialog();
	
	if(choice == -1)
		return;

	string params;


	params += "cd "; //the "cd " is not include by mani_test(int)
	
	//if it is really installed	
	installed = false;
	metamod_test(server[choice]);
	params += param;
	//-------end---------(modified)


	

	if(installed==true)
	{
		cout << _("\n\nDeactivating metamod...\nDeleting metamod.vdf");
		params += " && rm -rf addons/metamod.vdf";


		system_check(system(params.c_str()));
	}
}


//TODO: find a way to implement aliases like supported in metamod
//eg sm /path/to/sourcemod -> now sm is alias
void metaplugins_ini()
{
	bool exit=false;

	string params;

	cout << _("\n\nFrom which server do you want to manage the Plugins?\n");

	int choice = choose_dialog();
	
	if(choice == -1)
		return;


	//if it is really installed	
	installed = false;
	metamod_test(server[choice]);
	params += param;
	//-------end---------



	if(installed==true)
	{


		params += "/addons/metamod/metaplugins.ini";

		



		while(!exit)
		{

			int counter = 0;
			vector<string> filter;

			ifstream in;
			in.open(params.c_str());
		
			cout << _("\nNOTE: this file is only needed when plugins do not come with a *.vdf file\n");
			cout << _("\nhere are your metamod plugins:  \n");

			if(in.is_open())
			{
				while (!in.eof())
				{
					

					getline(in, temp);

					switch(temp[0])
					{
						case ';': //nothing to do
							break;
						case 10: //nothing to do (10 means new line)
							break;
						case 0: //nothing to do (0 means end of string)
							break;
						case 13: break;
						default: filter.push_back(temp);
							cout << (counter + 1) << ".\t" << filter[counter] << endl;
							counter++; 
							break;
					}
				}
			}
			else
				cout << _("\nNo plugins. Please add one!\n");

			cout << _("\n\n1. Add Plugin\n");
			cout << _("2. Delete Plugin\n");
			cout << _("3. Delete complete list\n");
			cout << _("4. Exit\n");
			cout << " : ";
			cin >> choice;
			cin.ignore();

			switch (choice)
			{
				case 1: cout << _("\nPlugin Name: ");
				{
					
					getline(cin, temp);
					ofstream out;
					out.open(params.c_str(), ios_base::app);
					out << endl << temp;
					out.close();
				}
				break;


				case 2: cout << _("\n\nWhich Plugin do you want do delete? ");
				cin >> choice;
		
				choice--;

				if(choice >= counter)
				{
					cout << _("\nThis entry does not exist!\n\n");
				}
				else
				{

					ofstream save_del;
					save_del.open(params.c_str());
					
					
					filter.erase(filter.begin() + choice);

					for (int i = 0;i<filter.size();i++)
						save_del << endl << filter[i];
						
					save_del.close();
				}
				break;
				case 3: char temp;
				cout << _("\n\nThe hole Plugin list is going to be deleted. Are you sure?(y|n) ");
				cin >> temp;
					

				if(temp=='y')
				{
					ofstream del_all;
					del_all.open(params.c_str());
					del_all.close();
				}
				break;
				case 4: exit=true;
				break;
				default: cout << _("\n\nTry again;)\n\n");
				break;
			}
		}
		
	}
}

void config_metamod()
{
	bool exit=false;

	while(!exit)
	{
		unsigned short int choice;
		cout << _("\n\n1. Install metamod\n");
		cout << _("2. Deactivate metamod\n");
		cout << _("3. Metamod Plugins\n");
		cout << _("4. Back\n");
		cout << " : ";
		cin  >> choice;	

		switch(choice)
		{
			case 1: install_metamod();
			break;
			case 2: deactivate_metamod();
			break;
			case 3: metaplugins_ini();
			break;
			case 4: exit=true;
			break;
			default: cout << _("\n\nWrong choice. Try again;)");
			break;
		}
	}
}